package com.tanhua.app.controller;

import com.aliyuncs.exceptions.ClientException;
import com.tanhua.app.service.UserService;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping(value = "/saveUser",name = "保存用户")
    public ResponseEntity saveUser(@RequestBody User user){
        return userService.saveUser(user);
    }

    //根据手机号做查询
    @GetMapping(value = "/findByPhone",name = "根据手机号查询用户")
    public ResponseEntity findByPhone(String phone){
        return userService.findByPhone(phone);
    }



    @PostMapping(value = "/login",name = "发送短信验证码")
    public ResponseEntity login(@RequestBody Map param) throws ClientException {
        // 获取传递过来的json数据拿到手机号
        String phone = (String)param.get("phone");
        return userService.login(phone);
    }


    @PostMapping(value = "/loginVerification",name = "注册/登录")
    public ResponseEntity loginVerification(@RequestBody Map param) throws ClientException {
        //获取登录的手机号
        String phone = (String)param.get("phone");
        String vCode = (String)param.get("verificationCode");
        //获取给该手机发送的验证码
        return userService.loginVerification(phone,vCode);
    }




    @PostMapping(value = "/loginReginfo",name = "保存新用户个人完善信息")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) throws ClientException {

        return userService.loginReginfo(userInfo);
    }


    @PostMapping(value = "/loginReginfo/head",name = "更新新用户个人完善信息的头像")
    public ResponseEntity loginReginfoPic(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws ClientException, IOException {
       return userService.loginReginfoPic(headPhoto,token);
    }


}
